package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.materialBomCommand.AddMaterialBomCommand;
import com.woniu.command.materialBomCommand.DeleteMaterialBomCommand;
import com.woniu.command.materialBomCommand.UpdateMaterialBomCommand;
import com.woniu.inlet.vo.MaterialBomVo;
import com.woniu.outlet.po.MaterialBomPo;
import com.woniu.service.MaterialBomService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "物料BOM管理")
@Slf4j
@RequestMapping("/materialBom/")
public class MaterialBomController {

    @Autowired
    private MaterialBomService materialBomService;

    @GetMapping("/materialBoms")
    @ApiOperation("查询物料BOM")
    public ResponseResult<List<MaterialBomVo>> findMaterialBoms() throws Exception{
        List<MaterialBomPo>  materialBomPos =  materialBomService.findMaterialBoms();
        List<MaterialBomVo> materialBomVos = new ArrayList<>();
        //对象的深拷贝
        for (MaterialBomPo item : materialBomPos){
            MaterialBomVo materialBomVo = new MaterialBomVo();
            BeanUtils.copyProperties(item,materialBomVo);
            materialBomVos.add(materialBomVo);
        }
        return new ResponseResult<>(materialBomVos, "查询成功",ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/materialBoms/id")
    @ApiOperation("根据id查询物料BOM")
    public ResponseResult<MaterialBomVo> getMaterialBomById(Long id) throws Exception {
        MaterialBomPo materialBomPo = materialBomService.getMaterialBomById(id);

        //对象的深拷贝

        MaterialBomVo materialBomVo = new MaterialBomVo();
        BeanUtils.copyProperties(materialBomPo, materialBomVo);


        return new ResponseResult<>(materialBomVo,"查看成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/materialBoms/getMaterialBomByMaterialCode")
    @ApiOperation("根据物料编码查询物料BOM")
    public ResponseResult<MaterialBomVo> getMaterialBomByMaterialCode(String materialCode) throws Exception {
        MaterialBomPo materialBomPo = materialBomService.getMaterialBomByMaterialCode(materialCode);

        //对象的深拷贝

        MaterialBomVo materialBomVo = new MaterialBomVo();
        BeanUtils.copyProperties(materialBomPo, materialBomVo);

        return new ResponseResult<>(materialBomVo,"查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加物料BOM")
    @ApiOperationSupport(ignoreParameters = {"id","materialCode","spare1","spare2","spare3"})
    public ResponseResult<String> addMaterialBom(MaterialBomPo materialBomPo){
        Command command = new AddMaterialBomCommand();
        command.execute(materialBomPo);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改物料BOM")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateMaterialBom(MaterialBomPo materialBomPo){
        Command command = new UpdateMaterialBomCommand();
        command.execute(materialBomPo);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除物料BOM")
    public ResponseResult<String> deleteMaterialBom(Long id){
        Command command = new DeleteMaterialBomCommand();
        command.execute(id);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
